<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-20
 * Time: 上午9:49
 */
namespace Service\Controller;
use Mcit\Page;
//在线电桩
class OnlineController extends BaseController {

    public function index(){

        $char_o =M("char")->where(1)->select();
        $area = session("area");//当前运营商区号
        $strs = '';
        foreach($char_o as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        //当前运商下所有电桩
        $Map['key_link'] = array('IN',rtrim($strs,','));
        $online  = D("Admin/Online")->where($Map)->select();
        $onlines = '';
        foreach($online as $v){
            //小于5分钟为故障或下线 5分钟 = 300秒
            /*if((strtotime("now")-strtotime($v['time_last'])) <  300){
                $onlines .= $v['key_link'].",";
            }*/
            $onlines .= $v['key_link'].",";
        }
        $map['key_link'] = array('IN',rtrim($onlines,','));
        if(!empty($_GET['evc_number'])){
            $map['evc_number'] = array('like','%'.I('evc_number').'%');
        }
        //分页
        $count = M("char")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $ev_char = D("Admin/Char")->Relation(true)->where($map)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //echo D("Char")->getLastSql();
        //分页结束
        //$sss = '';
        foreach($ev_char as $k=>$v){
            //$sss .= $v['key_link'].",";
            $onl  = D("Admin/Online")->where("key_link='".$v['key_link']."'")->find();
            if((strtotime("now")-strtotime($onl['time_last'])) >  40){
                $ev_char[$k]['status'] = '<span style="color: #cc6600">异常</span>';
            }else if((strtotime("now")-strtotime($onl['time_last'])) >  600){
                $ev_char[$k]['status'] = '<span style="color: red">下线</span>';
            }else{
                $ev_char[$k]['status'] = '<span style="color: #009900">在线</span>';
            }
            if($v['plantinfo'] == ''){
                $key_link = substr($v['key_link'],0,12);
                $v['plantinfo'] = M("plant")->where("station_key='".$key_link."' or id='".$v['ev_plant_id']."'")->find();
            }
            $region_arr = strsToArray($v['plantinfo']['location']);//查所在地
            $region = D("Admin/Region")->where("code='".$region_arr[2]."'")->getField('fullname');
            $ev_char[$k]['plant_name'] = $v['plantinfo']['name'];//站点名称
            if(mb_strlen($v['plantinfo']['name'],'utf-8') <= 17){
                $ev_char[$k]['plant_name_text'] = $v['plantinfo']['name'];
            }else{
                $ev_char[$k]['plant_name_text'] = mb_substr($v['plantinfo']['name'],0,17,"utf-8")."···";
            }
            $ev_char[$k]['region_text'] = $region;
        }
        //P($ev_char);
        $this->assign('ev_char',$ev_char);
        $this->display();
    }
    //查看电桩详细信息
    public function detail(){
        if(IS_GET){
            $data         = M("char")->where("id='".$_GET['id']."'")->find();
            $key_link     = substr($data['key_link'],0,12);
            $plant        = M("plant")->where("id='".$data['ev_plant_id']."' or station_key='".$key_link."'")->field("location,name,station_key")->find();
            $region       = strsToArray($plant['location']);
            $region_text  = D("Region")->where("code='".$region[2]."'")->getField("fullname");
            $data['name'] = $plant['name'];
            $data['location_text'] = $region_text;
            $data['station_key']   = $plant['station_key'];
            $trademark = M("fac")->where("id=".$data['ev_fac_code'])->find();
            $fac_type1 = M("device_type")->where("id=".$data['ev_fac_type1'])->find();
            $fac_type2 = M("device_type")->where("id=".$data['ev_fac_type2'])->find();
            $data['trademark']     = $trademark['fac_name'];
            $data['ev_fac_code']   = $trademark['name'];
            $data['ev_fac_type1']  = $fac_type1['name'];
            $data['ev_fac_type2']  = $fac_type2['name'];
            $this->data = $data;
        }
        $this->display();
    }
}